// JavaScript Document
jQuery.fn.renderMenu = function(options){
	// target the UL!
	var defaults = {
		container:'#menu',
		currentSection:''
	}
	// combine user defaults and our defaults
	var options = $.extend({},defaults,options);
	
	// convert the menu into an object we can use
	var menuObj = $(this).parseMenu(options);
	
	// now clear the space for the menu
	$(this).parent().css({'height':'50px'});
	$(this).remove();
	
	// now rebuild it...
	var target = $(options.container);
	target.addClass('dropDownContainer');
	var newUL = $('<ul/>',{
		id:'dropDownMenu'
	}).appendTo(target);
	
	var mainMenuItem_width = 100/menuObj.length;
	for(i=0;i<menuObj.length;i++){
		var mainMenuItem = $('<li/>',{
			'class':'levelOneBttn button_'+i,
			width:mainMenuItem_width+'%',
			html:'<a href="'+menuObj[i].link+'" title="'+menuObj[i].text+'" target="_self">'+menuObj[i].text+'</a>'
		}).appendTo(newUL);
	}
	
	
	// now bind a mouseover behavior
	$('#dropDownMenu .levelOneBttn a').bind('mouseover',function(){
		//if(!$(this).hasClass('hover')){
			globalRemoveMenu();
		//}
		$(this).addClass('hover');
		var rollOver = $('<div/>',{
			css:{
				position:'absolute',
				left:$(this).offset().left+'px',
				top:($(this).offset().top + 50)+'px' ,
				'background-color':'#80AACB'
			},
			'class':'hoverDropDown'
		}).appendTo( $('body') );
		
		// now we need to create the items in the dropdown
		var subMenu = $('<ul/>',{
			'class':'subMenu'
		}).appendTo( rollOver );
		
		var elementIndex = $(this).parent('li').index();
		for(i=0;i<menuObj[elementIndex].kids.length;i++){
			var listIten = $('<li/>',{
				'class':'subMenu_item',
				html:'<a href="'+menuObj[elementIndex].kids[i].link+'" title="'+menuObj[elementIndex].kids[i].text+'" target="_self">'+menuObj[elementIndex].kids[i].text+'</a>',
				css:{
					padding:'0 12px 0 0'
				}
			}).appendTo( subMenu );
		}
		// bind an event to the hoverDropDown element
		$('.hoverDropDown, .hoverDropDown .subMenu, .hoverDropDown .subMenu li, .hoverDropDown .subMenu li a').bind('mouseover',{parentCaller:$(this)},resetMenu);
		$('.hoverDropDown, .hoverDropDown .subMenu, .hoverDropDown .subMenu li, .hoverDropDown .subMenu li a').bind('mouseout',{parentCaller:$(this),delay:500},subMenu_removeMenu);
	}).bind( 'mouseout',{delay:500},removeMenu); 
	
	// current menu item...
	if(options.currentSection.length > 0){
		$('.levelOneBttn:econtains("'+options.currentSection+'")').addClass('currentSection');
	}
}
/*
=============================================================
Parse the menu into an object of nested objects and arrays */
jQuery.fn.parseMenu = function(options){
	var defaults = {
		levels:1
	}
	// combine user defaults and our defaults
	var options = $.extend({},defaults,options);
	
	function checkForKids(listRef,menuArrRef){
		var arrayRef = menuArrRef;
		// find all the LIs in the UL we passed
		listRef.contents('li').each(function(){
			var this_a = $(this).contents('a:first');
			var tmpItem = {
				text: this_a.text(),
				link: this_a.attr('href'),
				kids: new Array()
			}
			arrayRef.push(tmpItem);
			if( $(this).contents('ul').length > 0 ){
				checkForKids($(this).contents('ul'),tmpItem.kids);
			}
		});
		return arrayRef;
	}
	
	var menu = new Array();
	var menuObj = checkForKids($(this),menu);
	
	return menuObj;
}
function globalRemoveMenu(){
	$('.hover').clearQueue();
	$('.levelOneBttn').clearQueue();
	$('.hoverDropDown').remove();
	$('.hover').removeClass('hover');
}
function removeMenu(event){
	$(this).delay(event.data.delay).queue(function () {
		$(this).removeClass('hover');
		$('.hoverDropDown').remove();
		$(this).dequeue();
	});
}
function resetMenu(event){
	var parentCaller = event.data.parentCaller;
	parentCaller.clearQueue();
	//parentCaller.trigger('mouseout',[{delay:1000}]);
}
function subMenu_removeMenu(event){
	var parentCaller = event.data.parentCaller;
	parentCaller.clearQueue();
	parentCaller.trigger('mouseout',[{delay:event.data.delay}]);
}

$.expr[":"].econtains = function(obj, index, meta, stack){
	return (obj.textContent || obj.innerText || $(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
}

